package com.shenzhen.horselive.common.utils;

import android.app.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.GenericLifecycleObserver;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;

import com.shenzhen.horselive.widget.CommonDialog;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.HashMap;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;

import static androidx.lifecycle.Lifecycle.State.DESTROYED;

@SuppressWarnings("RestrictedApi")
public class Permissions implements GenericLifecycleObserver {
    private Activity activity;
    private static HashMap<String, Permissions> cache = new HashMap<>();

    private Permissions(Activity activity) {

        this.activity = activity;
        if (activity instanceof AppCompatActivity) {
            ((AppCompatActivity) activity).getLifecycle().addObserver(this);
        }
    }

    public static final Permissions of(Activity activity) {
        Permissions permissions = cache.get(activity.toString());
        if (permissions != null) {
            return permissions;
        }
        permissions = new Permissions(activity);
        if (activity instanceof AppCompatActivity) {
            cache.put(activity.toString(), permissions);
        }
        return permissions;
    }

    public Observable<Boolean> request(final String denyText, final String... permissions) {
        return request(denyText, false, permissions);
    }

    public Observable<Boolean> request(final String denyText, final boolean force, final String... permissions) {
        RxPermissions rxPermissions = new RxPermissions(activity);
        return rxPermissions.request(permissions).flatMap((Function<Boolean, ObservableSource<Boolean>>) granted -> {
            if (granted) {
                return Observable.just(true);
//                            return granted;
            } else {
                return Observable.create((ObservableOnSubscribe<Boolean>) emitter -> {
                    CommonDialog denyDialog = new CommonDialog(activity, denyText);
                    if (force) {
                        denyDialog.setConfirmStyle(true);
                        denyDialog.setCancelable(false);
                    } else {
                        denyDialog.setCancelButtonText("取消");
                    }
                    denyDialog.setOnCancelButtonClickListener(dialog -> {
                        emitter.onNext(false);
                        emitter.onComplete();
                    });
                    denyDialog.setOnPositiveButtonClickListener(dialog -> {
                        emitter.onNext(false);
                        emitter.onComplete();
                    });
                    denyDialog.setPositiveButtonText("去开启");
                    if (!denyDialog.isShowing()) {
                        denyDialog.show();
                    }
                });

            }

        }).flatMap((Function<Boolean, ObservableSource<Boolean>>) granted -> {
            if (force && !granted) {
                return request(denyText, true, permissions);
            }
            return Observable.just(granted);
        });
    }


    @Override
    public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
        if (((AppCompatActivity)activity).getLifecycle().getCurrentState() == DESTROYED) {
            ((AppCompatActivity)activity).getLifecycle().removeObserver(this);
            cache.remove(activity.toString());
        }
    }
}
